from typing import Optional

from appium import webdriver
from loguru import logger
from selenium.common import StaleElementReferenceException
from selenium.webdriver.support.wait import WebDriverWait

from basic.basedriver import DRIVER


class BasePage:
    def __init__(self):
        self.driver = DRIVER

    def locator(self, loc, wait: int = None, freq: int = 0.5) -> Optional['WebElement']:
        # 元素定位
        return self.driver.find_element(*loc) if wait is None else \
            WebDriverWait(self.driver, wait, freq, [StaleElementReferenceException]).until(lambda x: x.find_element(*loc))

    def click(self,loc,wait:int=None,freq:int=0.5):
        logger.info(f"点击元素：{loc}")
        self.locator(loc,wait=wait,freq=freq).click()